package com.example.alladapter.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.alladapter.Datas.PicName;
import com.example.alladapter.R;

import java.util.ArrayList;

public class myDialog extends Dialog implements View.OnClickListener {
    private OnClickLeftListen onClickLeftListen;
    private OnClickRightListen onClickRightListen;
    private Context mContext;
    private ImageView imageView;
    private Button btn_left, btn_right;
    private TextView textView;
    private int pos;
    private ArrayList<PicName> arrayList;

    public myDialog(@NonNull Context context, int pos, ArrayList<PicName> arrayList) {
        super(context, 0);
        this.mContext = context;
        this.pos = pos;
        this.arrayList = arrayList;
    }

    public myDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    public myDialog setOnLeftClick(OnClickLeftListen onClickLeftListen) {
        this.onClickLeftListen = onClickLeftListen;
        return this;
    }

    public myDialog setOnRightListen(OnClickRightListen onClickRightListen) {
        this.onClickRightListen = onClickRightListen;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_showpic);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels);
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL);//内围区域底部显示
        imageView = findViewById(R.id.imgView);
        btn_left = findViewById(R.id.btn_left);
        btn_right = findViewById(R.id.btn_right);
        textView = findViewById(R.id.tv_ShowPicName);
        btn_left.setOnClickListener(this::onClick);
        btn_right.setOnClickListener(this::onClick);
        imageView.setImageResource(arrayList.get(pos).pic);
        textView.setText(arrayList.get(pos).string);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_left:
                if (onClickLeftListen != null) {
                    onClickLeftListen.OnLeftClick(this);
                }
                break;
            case R.id.btn_right:
                if (onClickRightListen != null) {
                    onClickRightListen.OnRightClick(this);
                }
                break;
        }
    }

    public myDialog setPicTitle(String s) {
        this.textView.setText(s);
        return this;
    }

    public myDialog setPicId(int Rid) {
        imageView.setImageResource(Rid);
        return this;
    }

    public interface OnClickLeftListen {
        Void OnLeftClick(myDialog myDialog);
    }

    public interface OnClickRightListen {
        Void OnRightClick(myDialog myDialog);
    }

}
